<?php
set_time_limit(0);
date_default_timezone_set("Asia/Shanghai");


class G7Base{

	/**
	 * 服务器地址
	 * http://g7.huoyunren.com/rest/index.php
	 * http://g7demo.huoyunren.com/rest/index.php
	 * @var string
	 */
	var $ServerUrl = "http://g7.huoyunren.com/rest/index.php";

	/**
	 * 调用api方法
	 * @var string
	 */
	var $APIMethod='';

	/**
	 * app key
	 * @var string
	 */
	var $AppKey = "kafa1234";

	/**
	 * app secret
	 * @var string
	 */
	var $AppSecret = "4691f18484653144e92202ec60a64a0d";

	/**
	 * [$Data description]
	 * @var array
	 */
	var $Data = array();

	var $GeoType = 1;

	/**
	 * 数据传递类型 
	 * 1为json，默认
	 * @var integer
	 */
	var $Format = 1;

	/**
	 * 时间戳格式
	 * @var string
	 */
	var $TimeFormat = "Y-m-d H:i:s";

	/**
	 * 当前时间戳
	 * 格式：yyyy-mm-dd hh:mm:ss
	 * @var string
	 */
	private $_TimeStamp='';

	/**
	 * 验证签名
	 * @var string
	 */
	private $_sign = '';




	function __construtor(){

	}

	/**
	 * 初始化各项属性
	 * @return NULL
	 */
	function init(){
		$this->_sign = '';
	}

	/**
	 * 根据地址获得经纬度
	 * @param  [type] $address [description]
	 * @return [type]          [description]
	 */
	function getGeoAddress($address){
		$this->init();

		$this->_getTime();
		$this->APIMethod = 'ips2.api.geoaddress';

		/*地址处理*/
		if(is_string($address)){ //单地址处理
			$tmp['addrid'] = 1;
			$tmp['address'] = $address;
			$tmp['geotype'] = $this->GeoType;
			$this->Data[] = $tmp;
		}else if(is_array($address)){ //多地址处理
			$index = 0;
			foreach($address as $add){
				$index++;
				$tmp['addrid'] = $index;
				$tmp['address'] = $add;
				$tmp['geotype'] = $this->GeoType;
				$this->Data[] = $tmp;
			}
		}else{
			return false;
		}

		/*生产验证签名*/
		$this->_makeSign();
		/*获得生产签名后参数*/
		$params = $this->_getParams();
		/*进行post请求*/
		$content = $this->_httpPost($this->ServerUrl,$params);
		$data = json_decode($content,1);

		if(!$data['code']){
			return $data['data']['result'][0];
		}else{
			return '';
		}
	}

	/**
	 * 获得当前时间
	 * @return string 格式为“yyyy-mm-dd hh:mm:ss”的时间戳
	 */
	private function _getTime(){
		$this->_TimeStamp = date($this->TimeFormat);
		return $this->_TimeStamp;
	}

	/**
	 * 获得请求的各项参数
	 * @return array
	 */
	private function _getParams(){
		$param['app_key'] = $this->AppKey;
		$param['timestamp'] = $this->_TimeStamp;
		$param['method'] = $this->APIMethod;
		$param['data'] = $this->Data;
		if($this->_sign !== ''){
			$param['sign'] = $this->_sign;
		}

		return $param;
	}

	/**
	 * curl模拟post请求
	 * @param  string $url    交互请求服务器地址
	 * @param  array $params 传递各项参数
	 * @return string     curl返回内容
	 */
	private function _httpPost($url,$params){
		$handle = curl_init();

		$params = $this->_http_build_query($params);

		curl_setopt($handle, CURLOPT_URL, $url);
		curl_setopt($handle, CURLOPT_TIMEOUT, 15);
		curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($handle, CURLOPT_POST, 1);
		curl_setopt($handle, CURLOPT_POSTFIELDS, $params);

		$content = curl_exec($handle);
		curl_close($handle);
		return $content;

	}

	/**
	 * 生成验证签名
	 * @return string 
	 */
	private function _makeSign(){
		$params = $this->_getParams();
		
		ksort($params);

		$signstr='';
		foreach ($params as $key => $value) {
			$signstr.=$key.(is_array($value)?json_encode($value):$value);
		}
		
		//根据算法拼接签名
		$signstr = $this->AppSecret.$signstr.$this->AppSecret;
		$this->_sign = md5($signstr);
		$this->_sign = strtoupper($this->_sign);

		return $this->_sign;
	}

	/**
	 * 生成post字符串
	 * @param  array $data 格式化为字符串数据
	 * @return string    
	 */
	private function _http_build_query($data){
		$query='';
		foreach($data as $k=>$v){
			if($k=='data'){
				$v = is_array($v)?json_encode($v):$v;
				$query.="$k=".urlencode($v)."&";
			}else{
				$query.="$k=".$v."&";
			}
		}
		$query = $query != ''?trim($query,'&'):'';
		return $query;
	}
}